package com.jinzhi.jzweb.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.common.utils.Result;
import com.jinzhi.jzweb.domain.InvoiceDO;
import com.jinzhi.jzweb.service.InvoiceService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;

/**
 * 
 * <pre>
 * 用户发票信息
 * </pre>
 * <small> 2019-12-11 15:50:57 | lance</small>
 */
@Controller
@RequestMapping("/jzweb/invoice")
public class InvoiceController extends BaseController {
	@Autowired
	private InvoiceService invoiceService;
	
	@GetMapping()
	@RequiresPermissions("jzweb:invoice:invoice")
	String Invoice(){
	    return "jzweb/invoice/invoice";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("jzweb:invoice:invoice")
	public Result<Page<InvoiceDO>> list(InvoiceDO invoiceDTO){
        Wrapper<InvoiceDO> wrapper = new EntityWrapper<InvoiceDO>().orderBy("id", false);
        //wrapper.eq("name","jzweb");
        Page<InvoiceDO> page = invoiceService.selectPage(getPage(InvoiceDO.class), wrapper);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("jzweb:invoice:add")
	String add(){
	    return "jzweb/invoice/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("jzweb:invoice:edit")
	String edit(@PathVariable("id") Long id,Model model){
		InvoiceDO invoice = invoiceService.selectById(id);
		model.addAttribute("invoice", invoice);
	    return "jzweb/invoice/edit";
	}
	
	@Log("添加用户发票信息")
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("jzweb:invoice:add")
	public Result<String> save( InvoiceDO invoice){
		Date date = new Date();
		invoice.setCreateDate(date);
		invoice.setCreator(this.getUsername());
		invoice.setUpdateDate(date);
		invoice.setUpdater(this.getUsername());
		boolean save = invoiceService.insert(invoice);
        return save ? Result.ok() : Result.fail();
	}
	
	@Log("修改用户发票信息")
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("jzweb:invoice:edit")
	public Result<String>  update( InvoiceDO invoice){
		invoice.setUpdateDate(new Date());
		invoice.setUpdater(this.getUsername());
		boolean update = invoiceService.updateById(invoice);
		return update ? Result.ok() : Result.fail();
	}
	
	@Log("删除用户发票信息")
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("jzweb:invoice:remove")
	public Result<String>  remove( Long id){
		invoiceService.deleteById(id);
        return Result.ok();
	}
	
	@Log("批量删除用户发票信息")
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("jzweb:invoice:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		invoiceService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}
	
}
